package cn.lopr.im.android.mvp.presenter;

import android.text.TextUtils;

import java.util.List;

import cn.lopr.im.android.bean.Article;
import cn.lopr.im.android.bean.ArticleCommentVo;
import cn.lopr.im.android.bean.ArticlePlate;
import cn.lopr.im.android.bean.ArticleVo;
import cn.lopr.im.android.bean.CallbackResult;
import cn.lopr.im.android.bean.PageInfo;
import cn.lopr.im.android.common.OnCallBackListener;
import cn.lopr.im.android.mvp.contact.ICommunityContact;
import cn.lopr.im.android.mvp.model.CommunityModel;

/**
 * @author shuangyang7
 * @date 2020/12/19 11:51
 */
public class CommunityPresenterImpl implements ICommunityContact.ICommunityPresenter {

    private static final String TAG = "ss";
    private final ICommunityContact.ICommunityView communityView;
    private final ICommunityContact.ICommunityModel communityModel;

    public CommunityPresenterImpl(ICommunityContact.ICommunityView communityView) {
        this.communityView = communityView;
        this.communityModel = new CommunityModel();
    }

    @Override
    public void getPlates(String token) {
        this.communityModel.getPlates(token, new OnCallBackListener<CallbackResult<List<ArticlePlate>>, String>() {
            @Override
            public void success(CallbackResult<List<ArticlePlate>> data) {
                communityView.getPlatesSuccess(data.getData());
            }

            @Override
            public void failure(String errMsg) {
                communityView.showVerifyFailed(errMsg);
            }
        });
    }

    @Override
    public void getArticleList(String token, String plateId, int page, int size) {
        if (TextUtils.isEmpty(plateId) || plateId.equals("全部")) {
            plateId = "";
        }
        this.communityModel.getArticleList(token, plateId, page, size,
                new OnCallBackListener<CallbackResult<PageInfo<ArticleVo>>, String>() {
                    @Override
                    public void success(CallbackResult<PageInfo<ArticleVo>> data) {
                        communityView.getArticleSuccess(data.getData());
                    }

                    @Override
                    public void failure(String errMsg) {
                        communityView.showVerifyFailed(errMsg);
                    }
                });
    }

    @Override
    public void delArticle(String token, String articleId) {
        communityModel.delArticle(token, articleId, new OnCallBackListener<CallbackResult<String>, String>() {
            @Override
            public void success(CallbackResult<String> data) {

            }

            @Override
            public void failure(String errMsg) {

            }
        });
    }

    @Override
    public void updateArticleLikeNum(String token, String articleId, int num) {
        Article article = new Article().setId(articleId).setLike(num);

        this.communityModel.updateArticle(token, article, new OnCallBackListener<CallbackResult<String>, String>() {
            @Override
            public void success(CallbackResult<String> data) {
                communityView.updateArticleSuccess(data.getData());
            }

            @Override
            public void failure(String errMsg) {
                communityView.showVerifyFailed(errMsg);
            }
        });
    }

    @Override
    public void getArticleComment(String token, String articleId, int page, int size) {
        this.communityModel.getArticleComment(token, articleId, page, size,
                new OnCallBackListener<CallbackResult<List<ArticleCommentVo>>, String>() {
                    @Override
                    public void success(CallbackResult<List<ArticleCommentVo>> data) {
                        communityView.getArticleCommentSuccess(data.getData());
                    }

                    @Override
                    public void failure(String errMsg) {
                        communityView.showVerifyFailed(errMsg);
                    }
                });
    }


}
